var app = angular.module('myApp', []);

app.controller('startExamController', function($rootScope,$scope,$http,$location) {
	var getBasePath = globalPath.getBasePath;
	
	var questions = [];

    $scope.questions = questions;

    $scope.examId = 0;

    $scope.init = function() {
        $scope.rightCount = 0;
        var userId = localStorage.getItem('userId');

        if (!userId) {
            alert("您未登录,不能考试!");
            location.href = getBasePath() + "login";
            return ;
        }

        if (isNaN(userId)) {
            alert("用户ID数据错误,不能考试!请重新登录!");
            location.href = getBasePath() + "login";
            return ;
        }

        $http({
            method : 'GET',
            url : getBasePath() + 'user/back_start_exam',
            params : {
                "userId" : userId
            },
        }).
        success(function(response) {
            console.log("success");
            if (response.isSuccess) {
                var respData = response.data;
                console.log(response);
                console.log(respData);
                questions = respData.questionList;

                $scope.examId = respData.examId;
                $scope.questions = questions;
            }
        }).
        error(function(response) {
            console.log("error");
            console.log(response);
        });
    }

    $scope.init();

    $scope.rightCount == 0;

    console.log("答对了几题: " + $scope.rightCount);

    var qList = [];

    Array.prototype.contains = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] === obj) {
             return true;
            }
        }
        return false;
    }

    Array.prototype.indexOf = function(val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };

    Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };

    $scope.judge = function(qId,answer,select) {
        console.log(qId);
        console.log(answer);
        console.log(select);

        if (answer == select) {
            if (!qList.contains(qId)) {
                qList.push(qId);
                $scope.rightCount++;
            }
        }

        if (answer != select) {
            if (qList.contains(qId)) {
                qList.remove(qId);
                $scope.rightCount--;
            }
        }
    }

    //交卷
    $scope.finishPaper = function() {
        var cfm = confirm("确认交卷吗?");
        if (!cfm) {
            return ;
        }

        var userId = getLocalUserID();

        console.log("用户ID:" + userId);

        var localExamId = $scope.examId;

        console.log("考试号:" + localExamId);

        var qs = $scope.questions;
        var userAnswers = [];
        for (var i=0; i<qs.length; i++) {
            var tempQstn = {
                questionId : qs[i].questionId,
                userAnswer : qs[i].userAnswer,
                questionAnswer : qs[i].questionAnswer
            }
            userAnswers.push(tempQstn);
        };

        console.log(userAnswers);

        for (var i = userAnswers.length - 1; i >= 0; i--) {

            if (userAnswers[i].userAnswer === '' || userAnswers[i].userAnswer === undefined) {
                alert("您有题目未做完，不能提交！");
                return ;
            }
        };

        var formdata = {
        	examId : localExamId,
            questionList : userAnswers
        };


        $.ajax({
            type: "POST",
            url: getBasePath() + "exam/back_finish",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            dataType: "json",
            success: function (response) {
                console.log("success");
                var msg = response.msg + ",您的分数为: " + response.data;
                if (response.isSuccess) {
                	alert(msg);
                    location.href = getBasePath() + "user/center";
                } else {
                	alert(response.msg);
                }
            },
            error: function (message) {
                console.log("error");
            }
        });

        console.log("即将交卷!");
    }

    function getLocalUserID() {
        var userId = localStorage.getItem('userId');

        if (!userId) {
            alert("您未登录,不能考试!");
            location.href = getBasePath() + "login";
            return ;
        }

        if (isNaN(userId)) {
            alert("用户ID数据错误,不能考试!请重新登录!");
            location.href = getBasePath() + "login";
            return ;
        }

        return userId;
    }

    
});

